package ar.edu.utn.frgp.mvc.servlets;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.edu.utn.frgp.banco.domain.Servicio;
import ar.edu.utn.frgp.beans.MessageBean;
import ar.edu.utn.frgp.beans.ServicioBean;
import ar.edu.utn.frgp.banco.dao.Implements.ImpServicios;
import ar.edu.utn.frgp.banco.business.Implements.ImpPagosBusiness;

/**
 * Servlet implementation class PagoServiciosServlet
 */
public class PagoServiciosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PagoServiciosServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		MessageBean ban = new MessageBean();

		try {
			if (request.getParameter("estado").equals("vacio")) {
				// cargo el jsp
				ImpServicios imps = new ImpServicios();
				List<Servicio> lista_servicios = null;
				ServicioBean sb = new ServicioBean();

				lista_servicios = imps.TraeServicios();

				sb.setServicio(lista_servicios);
				ban.setMensaje("1");

				request.setAttribute("lista_servicios", sb);
				request.setAttribute("ban", ban);

				request.getRequestDispatcher("Servicios.jsp").forward(request,
						response);
			}
			if (request.getParameter("abonar").equals("Pagar")) {
				// hago el pago de un servicio
				ImpPagosBusiness pb = new ImpPagosBusiness();
				int ID_Cliente = (Integer.parseInt(request.getSession()
						.getAttribute("ID_Cliente").toString()));
				int ID_Cuenta = (Integer.parseInt(request.getSession()
						.getAttribute("ID_Cuenta").toString()));
				int ID_Servicio= (Integer.parseInt(request.getParameter("rb_servicio").toString()));

				pb.RealizarPagoServicio(ID_Cliente, ID_Cuenta, ID_Servicio);
				request.getRequestDispatcher("../../Blanco.jsp").forward(
						request, response);

			}

		} catch (Exception e) {
			MessageBean aux = new MessageBean();
			aux.setMensaje(e.getMessage());
			request.setAttribute("error", aux);
			
		}
	}
}
